package com.airclick.merchant.controller;

import com.airclick.merchant.entity.Commercial;
import com.airclick.merchant.service.CommercialService;
import com.airclick.merchant.service.MerService;
import com.airclick.merchant.service.TransactionDataService;
import com.airclick.merchant.utils.CommonResult;
import com.airclick.merchant.utils.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping({"/api/commercialtenant"})
@Api(value = "商户接口", tags = {"商户"})
@ApiResponses(value = {
        @ApiResponse(code = 0000, message = "" ,response = Commercial.class)
})
public class CommercialController {

    @Resource
    private MerService merService;

    @Resource
    private CommercialService commercialService;

    @Resource
    private TransactionDataService transactionDataService;


    @PostMapping(value = "/transactionData")
    @ApiOperation("商户后台首页")
    public Object transactionData(String adminId,String days){

        Map<String,Object> map = new HashMap<>();
        map = transactionDataService.selectacount(adminId,days);

        return CommonResult.success(map);
    }



    @PostMapping(value = "/selectInformation")
    @ApiOperation("查看账号信息")
    public CommonResult selectInformation(String merchantId){

        Commercial count = commercialService.selevtInformation(merchantId);

        if (count != null) {
            return CommonResult.success(count);
        }else {
            return CommonResult.failed(Constants.ERROR_NO_EXIST_MSG);
        }
    }


    @PostMapping(value = "/accountInformation")
    @ApiOperation("修改账号信息")
    public CommonResult accountInformation(String logo, String contactAddress, String contactNumber, String electronicMail ,String id ,String merchantName){

        int count = commercialService.accountInformation(logo,contactAddress,contactNumber,electronicMail,id,merchantName);

        if (count > 0) {
            return CommonResult.success(Constants.SUCCESS_MSG);
        }else {
            return CommonResult.failed(Constants.ERROR_MSG);
        }
    }





    @PostMapping(value = "/changepassword")
    @ApiOperation("修改密码")
    public CommonResult changepassword(String userName , String password ,String newpassword){


        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //查询有没有这个账号
        String merDomain = merService.findAllByUsername(userName);

        if (merDomain == null) {
            return CommonResult.failed(Constants.ACCOUNT_NUMBER_ERROR_NO_EXIST_MSG);
        }else {
            //判断原密码是否正确
            if (passwordEncoder.matches(password , merDomain)) {
                //新密码加密
                String encodePassword = passwordEncoder.encode(newpassword);
                //修改密码
                int count = merService.updatePassword(userName,encodePassword);
                //判断是否修改成功
                if (count > 0) {
                    return CommonResult.success(Constants.SUCCESS_MSG);
                }else{
                    return CommonResult.failed(Constants.ERROR_MSG);
                }
            }else{
                return CommonResult.failed(Constants.THE_OLD_PASSWORD_IS_INCORRECT);
            }
        }
    }


    @PostMapping(value = "/theDailyStatistics")
    @ApiOperation("查询7天或30天的记录")
    public CommonResult theDailyStatistics(String date){
        List<String> merList = merService.theDailyStatistics(date);

        if (merList == null) {
            return CommonResult.success(merList);
        }else {
            return CommonResult.success(merList);
        }

    }

    @PostMapping(value = "/selectSaveUser")
    @ApiOperation("当日新增的商户")
    public CommonResult selectSaveUser(){
        List<String> merList = merService.selectSaveUser();

        if (merList == null) {
            return CommonResult.success(merList);
        }else {
            return CommonResult.success(merList);
        }

    }


}
